package cn.iocoder.yudao.module.crm.service.coursepackage;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.course.vo.CoursePackagePageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.course.vo.CoursePackageRltSaveReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.course.vo.CoursePackageSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.coursepackage.CoursePackageDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.coursepackagerlt.CoursePackageRltDO;
import cn.iocoder.yudao.module.crm.dal.mysql.coursepackage.CoursePackageMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.coursepackagerlt.CoursePackageRltMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.subject.SubjectMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.COURSE_PACKAGE_NOT_EXISTS;
import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * 课程套餐 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CoursePackageServiceImpl implements CoursePackageService {

    @Resource
    private CoursePackageMapper coursePackageMapper;
    @Resource
    private CoursePackageRltMapper coursePackageRltMapper;
    @Resource
    private SubjectMapper subjectMapper;

    @Override
    public Long createCoursePackage(CoursePackageSaveReqVO createReqVO) {
        // 插入
        CoursePackageDO coursePackage = BeanUtils.toBean(createReqVO, CoursePackageDO.class);
        coursePackageMapper.insert(coursePackage);
        // 返回
        return coursePackage.getId();
    }

    @Override
    public void updateCoursePackage(CoursePackageSaveReqVO updateReqVO) {
        // 校验存在
        validateCoursePackageExists(updateReqVO.getId());
        // 更新
        CoursePackageDO updateObj = BeanUtils.toBean(updateReqVO, CoursePackageDO.class);
        coursePackageMapper.updateById(updateObj);
    }

    @Override
    public void deleteCoursePackage(Long id) {
        // 校验存在
        validateCoursePackageExists(id);
        // 删除
        coursePackageMapper.deleteById(id);
    }

    private void validateCoursePackageExists(Long id) {
        if (coursePackageMapper.selectById(id) == null) {
            throw exception(COURSE_PACKAGE_NOT_EXISTS);
        }
    }

    @Override
    public CoursePackageDO getCoursePackage(Long id) {
        return coursePackageMapper.selectById(id);
    }

    @Override
    public PageResult<CoursePackageDO> getCoursePackagePage(CoursePackagePageReqVO pageReqVO) {
        if(pageReqVO.getSubjectId()!=null&&pageReqVO.getSubjectId()>0){
            Set<Long> subjectIds = subjectMapper.selectSubjectIdListById(pageReqVO.getSubjectId());
            pageReqVO.setSubjectIds(subjectIds);
        }
        return coursePackageMapper.selectPage(pageReqVO);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCourse(CoursePackageRltSaveReqVO createReqVO) {
        CoursePackageDO updateObj = coursePackageMapper.selectById(createReqVO.getCoursePackageId());
        Set<Long> courseIds = Optional.ofNullable(updateObj.getCourseIds()).orElse(new HashSet<>());
        courseIds.addAll(createReqVO.getCourseIds());
        updateObj.setCourseIds(courseIds);
        coursePackageMapper.updateById(updateObj);
        //插入关系表
        if (CollectionUtil.isNotEmpty(createReqVO.getCourseIds())) {
            coursePackageRltMapper.insertBatch(convertList(createReqVO.getCourseIds(),
                    courseId -> new CoursePackageRltDO().setCoursePackageId(createReqVO.getCoursePackageId()).setCourseId(courseId)));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCourse(CoursePackageRltSaveReqVO createReqVO) {
        CoursePackageDO updateObj = coursePackageMapper.selectById(createReqVO.getCoursePackageId());
        Set<Long> courseIds = Optional.ofNullable(updateObj.getCourseIds()).orElse(new HashSet<>());
        courseIds.removeAll(createReqVO.getCourseIds());
        updateObj.setCourseIds(courseIds);
        coursePackageMapper.updateById(updateObj);
        //删除关系表
        if (CollectionUtil.isNotEmpty(createReqVO.getCourseIds())) {
            for (Long courseId : createReqVO.getCourseIds()) {
                coursePackageRltMapper.delete(new QueryWrapper<>(CoursePackageRltDO.builder()
                        .courseId(courseId).coursePackageId(createReqVO.getCoursePackageId()).build()));
            }
        }
    }
}